require "Logic/ShareManager"

local UIShare       = UIBase.New(PanelNames.UIShare)

local m_timer       = nil
local m_modelId     = nil
local m_shareType   = nil

local shareType = {
    Hero = 1,
    FriendInvited = 2
}

function UIShare.Init(shareType, hid)
    m_shareType = shareType
    m_modelId = hid
    UIBase.Init(UIShare)
end

function UIShare:OnCreate()
    self.list           = self:GetChild("listApp")
    self.ctr_showtips   = self:GetController("showReward")
    self.ctr_showtips.selectedIndex = ShareManager.HaveReward() and 0 or 1
    self.rewardTips     = self:GetChild("rewardTips")

    local reward = GetGlobalStringValue(537, true)
    if reward and reward[1][2] then
        self.rewardTips.text = string.format(getLanguage("ShareRewardTips"), reward[1][2])
    end

    if m_shareType == shareType.FriendInvited then
        self.ctr_showtips.selectedIndex = 1
    end
    
    self:RegisterOnClick(self.list:GetChildAt(0), self.onClick_Sina)
    self:RegisterOnClick(self.list:GetChildAt(1), self.onClick_WXCicle)
    self:RegisterOnClick(self.list:GetChildAt(2), self.onClick_WXFriend)
    self:RegisterOnClick(self:GetChild("btnBack"), self.onClick_ClosePanel)
end

function UIShare:onClick_ClosePanel()
    closeUI(PanelResNames.UIShareDraw)
    closeUI(PanelResNames.UIShareHero)
    closeUI(PanelResNames.UIShare)
end

function UIShare:onClick_WXCicle()
    if m_shareType == shareType.Hero then
        LoginSDK.GetInstance():ShareTextureToWXCircle("yanhuammo/share.jpg", ShareManager.GetDesc())
        self:ReqShareRewards()
    elseif m_shareType == shareType.FriendInvited then
        LoginSDK.GetInstance():ShareTextureToWXCircle("yanhuammo/share.jpg", "")
        local dic = {}
        dic.shareChannel = 1
        TAManager.TrackByDictionary("newPlayerShareChannel",dic)
    end
end

function UIShare:onClick_WXFriend()
    if m_shareType == shareType.Hero then
        LoginSDK.GetInstance():ShareTextureToWXFriend("yanhuammo/share.jpg", ShareManager.GetDesc())
        self:ReqShareRewards()
    elseif m_shareType == shareType.FriendInvited then
        LoginSDK.GetInstance():ShareTextureToWXFriend("yanhuammo/share.jpg", "")
        local dic = {}
        dic.shareChannel = 2
        TAManager.TrackByDictionary("newPlayerShareChannel",dic)
    end
end

function UIShare:onClick_Sina()
    if m_shareType == shareType.Hero then
        LoginSDK.GetInstance():ShareTextureSinaWB("yanhuammo/share.jpg", ShareManager.GetDesc())
        self:ReqShareRewards()
    elseif m_shareType == shareType.FriendInvited then
        LoginSDK.GetInstance():ShareTextureSinaWB("yanhuammo/share.jpg", "")
        local dic = {}
        dic.shareChannel = 3
        TAManager.TrackByDictionary("newPlayerShareChannel",dic)
    end
end

function UIShare:ReqShareRewards()
    -- if m_timer then
    --     utimer.remove(m_timer)
    --     m_timer = nil
    -- end

    -- m_timer = utimer.delayCall(function()
        ShareManager.Req_shareReward(m_modelId)
    -- end, 1500)
end

function UIShare:OnDestroy()
    if m_timer then
        utimer.remove(m_timer)
        m_timer = nil
    end
end

return UIShare